import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import Wlecome from "../components/Wlecome.vue";
import User from "../components/user/User.vue";
Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "Login",
      component: Login,
    },
    {
      path: "/home",
      name: "Home",
      component: Home,
      redirect:"/wlecome",
      children: [
        {
          path: "/wlecome",
          name: "Wlecome",
          component: Wlecome,
        },
        {
          path: "/wlecome",
          name: "Wlecome",
          component: Wlecome,
        },
      ],
    },
  ],
});
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  if (to.path === "/login") return next();
  const tokenList = JSON.parse(window.localStorage.getItem("token"));
  if (!tokenList) return next("/login");
  next();
});

export default router;
